package com.ruoyi.project.sale.customer.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sale.customer.domain.Customer;
import com.ruoyi.project.sale.customer.service.CustomerService;
import com.ruoyi.project.system.role.domain.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sale/customer")
public class CustomerController extends BaseController {
    @Autowired
    private CustomerService customerService;
/*test*/
    private String prefix = "sale/customer";

    @RequiresPermissions("sale:customer:view")
    @GetMapping()
    public String showCustomer()
    {
        return prefix + "/view";
    }

    @RequiresPermissions("sale:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer)
    {
        startPage();
        List<Customer> list = customerService.findAll(customer);
        return getDataTable(list);
    }


    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @RequiresPermissions("sale:customer:add")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Customer customer)
    {

        int index = customerService.saveCustomer(customer);
        try {
            if (index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return error("添加失败");

    }


    /**
     * 修改客户 数据回显
     */
    @GetMapping("/edit/{cid}")
    public String edit(@PathVariable("cid") Integer cid, ModelMap mmap)
    {
        mmap.put("customer", customerService.findOne(cid));
        return prefix + "/edit";
    }
    /**
     * 修改保存客户
     */
    @RequiresPermissions("sale:customer:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Customer customer)
    {
        Integer index = customerService.change(customer);
        try {
            if (index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage()); //将错误信息反馈给前台，e.printStackTrace();就要注销
        }
        return error("修改失败");
    }

    @RequiresPermissions("sale:customer:remove")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        Integer[] arr = Convert.toIntArray(ids);
        try {
            int index=0;
            if (arr.length==1){  //单条删除
               index= customerService.removeCustomer(arr[0]);
            }else {
                index=customerService.removeCustomers(arr);
            }
            if (index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return error("删除失败");


    }






}
